﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SubGroupService" in code, svc and config file together.

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class SubGroupService : ISubGroupService
    {
        private readonly SubGroupRepository SubGroupRepository;
        private WcfSessionStorage _session;
        public SubGroupService()
        {
            _session = new WcfSessionStorage();
            SubGroupRepository = new SubGroupRepository(_session);
        }

        #region ISubGroupService Members

        public List<SubGroupDTO> GetList(int page, out long recordCount, string sortProperty = null,
                                         string sortType = "ASC")
        {
            var Groups = new List<SubGroup>();
            var Results = new List<SubGroupDTO>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                Groups = SubGroupRepository.GetDataPaged(page, 30).ToList();
            }
            else
            {
                Groups = SubGroupRepository.GetDataPaged(page, 30, sortProperty, sortType).ToList();
            }
            recordCount = SubGroupRepository.RecordCount;
            Groups.ForEach(x =>
                               {
                                   var subGroupDto = new SubGroupDTO();
                                   Mapper.Map(x, subGroupDto);
                                   Results.Add(subGroupDto);
                               });
            return Results;
        }

        public bool Save(SubGroupDTO subGroupDto)
        {
            bool res;
            try
            {
                SubGroup subgroup = SubGroupRepository.GetDataOne(x => x.SubGroupID == subGroupDto.SubGroupID);
                var user = new User();
                if (subgroup == null)
                {
                    subgroup = new SubGroup();
                    subGroupDto.created_date = DateTime.Now;
                    //AutoMapper.Mapper.Map(subGroupDto.created_by, user);
                    //subGroupDto.modified_by = null;
                }
                else
                {
                    subGroupDto.modified_date = DateTime.Now;
                    //AutoMapper.Mapper.Map(subGroupDto.modified_by, user);
                }
                Mapper.Map(subGroupDto, subgroup);
                SubGroupRepository.SaveOrUpdate(subgroup);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public bool Delete(SubGroupDTO subGroupDto)
        {
            bool res =  false;
            try
            {
                var subgroup = new SubGroup();
                Mapper.Map(subGroupDto, subgroup);
                ItemRepository itemRepository = new ItemRepository(_session);
                var itemList = itemRepository.GetDataList(x => x.SubGroup.SubGroupID == subgroup.SubGroupID);
                if (itemList.Count > 0) return res;
                SubGroupRepository.Delete(subgroup);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public List<SubGroupDTO> GetListByCriteria(Dictionary<String, Object> criteria, string expression, int page,
                                                   out long recordCount)
        {
            var subGroups = new List<SubGroup>();
            var Results = new List<SubGroupDTO>();
            subGroups = SubGroupRepository.GetDataPaged(criteria, expression, page, 30).ToList();
            recordCount = SubGroupRepository.RecordCount;
            subGroups.ForEach(x =>
                                  {
                                      var subGroupDto = new SubGroupDTO();
                                      Mapper.Map(x, subGroupDto);
                                      Results.Add(subGroupDto);
                                  });
            return Results;
        }


        public List<SubGroupDTO> GetAllSubGroup()
        {
            List<SubGroup> subGroups = SubGroupRepository.FetchAll().ToList();
            IEnumerable<SubGroupDTO> Results = from s in subGroups
                                               select
                                                   new SubGroupDTO
                                                       {SubGroupID = s.SubGroupID, SubGroupName = s.SubGroupName};
            return Results.OrderBy(x=>x.SubGroupName).ToList();
        }

        public List<SubGroupDTO> GetSubGroupListByName(string SubgroupName)
        {
            IList<SubGroup> subGroups = SubGroupRepository.GetDataList(x => x.SubGroupName.Contains(SubgroupName));
            IEnumerable<SubGroupDTO> Results = from s in subGroups
                                               select
                                                   new SubGroupDTO
                                                       {SubGroupID = s.SubGroupID, SubGroupName = s.SubGroupName};
            return Results.ToList();
        }

        public bool SendEmail(SubGroupDTO subGroupDto,String DestinationFolder)
        {
            var mailServices = new MailServices();
            var subGroup = SubGroupRepository.GetDataOne(x => x.SubGroupID == subGroupDto.SubGroupID);
            subGroupDto = Mapper.Map(subGroup, subGroupDto);
            return mailServices.SendEmail(subGroupDto,DestinationFolder);
        }

        #endregion
    }
}